<template>
  <div class="visa-system">
    <div
      class="page-header"
      :style="bannerImage ? { backgroundImage: `url(${bannerImage})` } : {}"
      :class="{ 'has-bg': !!bannerImage }"
    >
      <div class="container">
        <h1 class="page-title">{{ "签证系统" }}</h1>
        <p class="page-subtitle">
          {{ pageData.subtitle || "为国际展商和观众提供签证服务" }}
        </p>
      </div>
    </div>
    <!-- 导航菜单 -->
    <div class="nav-menu">
      <div class="container">
        <nav class="about-nav">
          <a
            @click="setActiveTab('group-visit')"
            :class="['nav-item', { active: activeTab === 'group-visit' }]"
          >
            组团参观
          </a>
          <a
            @click="setActiveTab('visitor-preregistration')"
            :class="[
              'nav-item',
              { active: activeTab === 'visitor-preregistration' },
            ]"
          >
            观众预登记
          </a>
          <a
            @click="setActiveTab('visitor-notice')"
            :class="['nav-item', { active: activeTab === 'visitor-notice' }]"
          >
            观众须知
          </a>
          <a
            @click="setActiveTab('visa-system')"
            :class="['nav-item', { active: activeTab === 'visa-system' }]"
          >
            签证申请
          </a>
          <a
            @click="setActiveTab('visitor-roster')"
            :class="['nav-item', { active: activeTab === 'visitor-roster' }]"
          >
            展商名录
          </a>
          <a
            @click="setActiveTab('visitor-reviews')"
            :class="['nav-item', { active: activeTab === 'visitor-reviews' }]"
          >
            观众评语
          </a>
        </nav>
      </div>
    </div>

    <div class="container">
      <section class="visa-section">
        <!-- <h2 class="section-title">签证系统</h2> -->
        <div class="visa-content">
          <div class="visa-intro">
            <h3>签证服务</h3>
            <p>
              为国际展商和观众提供便捷的签证申请服务，确保您能够顺利参加展会。我们与相关机构合作，提供专业的签证咨询和办理服务。
            </p>
          </div>
          <div class="visa-services">
            <div
              class="service-item"
              v-for="service in visaServices"
              :key="service.id"
            >
              <div class="service-icon">
                <el-icon
                  ><component :is="getIconComponent(service.icon)"
                /></el-icon>
              </div>
              <div class="service-info">
                <h4>{{ service.title }}</h4>
                <p>{{ service.description }}</p>
              </div>
            </div>
          </div>
          <div class="visa-form">
            <h3>签证申请</h3>
            <form
              @submit.prevent="submitVisaApplication"
              class="visa-application-form"
            >
              <div class="form-row">
                <div class="form-group">
                  <label for="name">申请人姓名 *</label>
                  <input
                    type="text"
                    id="name"
                    v-model="visaForm.name"
                    required
                  />
                </div>
                <div class="form-group">
                  <label for="nameEn">英文姓名 *</label>
                  <input
                    type="text"
                    id="nameEn"
                    v-model="visaForm.nameEn"
                    required
                  />
                </div>
              </div>
              <div class="form-row">
                <div class="form-group">
                  <label for="passportNo">护照号 *</label>
                  <input
                    type="text"
                    id="passportNo"
                    v-model="visaForm.passportNo"
                    required
                  />
                </div>
                <div class="form-group">
                  <label for="nationality">国籍 *</label>
                  <input
                    type="text"
                    id="nationality"
                    v-model="visaForm.nationality"
                    required
                  />
                </div>
              </div>
              <div class="form-row">
                <div class="form-group">
                  <label for="birthDate">出生日期</label>
                  <input
                    type="date"
                    id="birthDate"
                    v-model="visaForm.birthDate"
                  />
                </div>
                <div class="form-group">
                  <label for="gender">性别 </label>
                  <select id="gender" v-model="visaForm.gender">
                    <option value="male">男</option>
                    <option value="female">女</option>
                  </select>
                </div>
              </div>
              <div class="form-row">
                <div class="form-group">
                  <label for="phone">联系电话</label>
                  <input type="text" id="phone" v-model="visaForm.phone" />
                </div>
                <div class="form-group">
                  <label for="email">邮箱 *</label>
                  <input
                    type="text"
                    id="email"
                    v-model="visaForm.email"
                    required
                  />
                </div>
              </div>
              <div class="form-row">
                <div class="form-group">
                  <label for="company">公司名称 *</label>
                  <input
                    type="text"
                    id="company"
                    v-model="visaForm.company"
                    required
                  />
                </div>
                <div class="form-group">
                  <label for="position">职位</label>
                  <input
                    type="text"
                    id="position"
                    v-model="visaForm.position"
                  />
                </div>
              </div>
              <div class="form-row">
                <div class="form-group">
                  <label for="entryDate">预计入境日期 *</label>
                  <input
                    type="date"
                    id="entryDate"
                    v-model="visaForm.entryDate"
                    required
                  />
                </div>
                <div class="form-group">
                  <label for="exitDate">预计出镜日期</label>
                  <input
                    type="date"
                    id="exitDate"
                    v-model="visaForm.exitDate"
                  />
                </div>
              </div>
              <div class="form-group">
                <label for="purpose">访问目的 *</label>
                <input
                  type="text"
                  id="purpose"
                  v-model="visaForm.purpose"
                  required
                />
              </div>
              <button type="submit" class="submit-btn">提交签证申请</button>
            </form>
          </div>
        </div>
      </section>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { subpageApi, visaApi } from "@/api/user";
import { Document, Clock, User, Location } from "@element-plus/icons-vue";

export default {
  name: "VisaSystem",
  setup() {
    const activeTab = ref("visa-system");
    // 页面基本信息
    const pageData = ref({});
    const bannerImage = ref("");

    // 签证服务数据
    const visaServices = ref([]);

    // 解析富文本内容
    const parseContent = (content) => {
      if (!content) return { content: "", backgroundImage: "" };

      try {
        if (typeof content === "string" && content.trim().startsWith("{")) {
          return JSON.parse(content);
        }
        return { content: content, backgroundImage: "" };
      } catch (e) {
        return { content: content, backgroundImage: "" };
      }
    };

    // 加载签证系统数据
    const loadVisaData = async () => {
      try {
        const response = await subpageApi.getSubpageList(
          "visitors",
          "visa-application"
        );
        if (
          response &&
          response.code === "200" &&
          response.data &&
          response.data.length > 0
        ) {
          // 取第一条作为页面基本信息
          const firstItem = response.data[0];
          pageData.value = {
            title: firstItem.title || "签证系统",
            subtitle: firstItem.description || "为国际展商和观众提供签证服务",
            content: firstItem.content || "",
          };

          // 处理多条签证服务数据
          visaServices.value = response.data.map((item, index) => {
            const parsed = parseContent(item.content);
            return {
              id: item.id || index + 1,
              title: item.title || "",
              description: parsed.content || "",
              icon: item.icon || getDefaultIcon(index),
              image: item.imageUrl || parsed.backgroundImage || "",
            };
          });
        } else {
          // 使用默认数据
          pageData.value = getDefaultPageData();
          visaServices.value = getDefaultVisaServices();
        }
      } catch (error) {
        console.error("加载签证系统数据失败:", error);
        pageData.value = getDefaultPageData();
        visaServices.value = getDefaultVisaServices();
      }
    };

    // 获取默认图标
    const getDefaultIcon = (index) => {
      const icons = ["Document", "Document", "User", "Clock"];
      return icons[index % icons.length];
    };

    // 默认页面数据
    const getDefaultPageData = () => {
      return {
        title: "签证系统",
        subtitle: "为国际展商和观众提供签证服务",
        content: "",
      };
    };

    // 默认签证服务数据
    const getDefaultVisaServices = () => [
      {
        id: 1,
        title: "签证咨询",
        description: "提供专业的签证政策咨询和申请指导服务",
        icon: "Document",
      },
      {
        id: 2,
        title: "材料准备",
        description: "协助准备签证申请所需的各类材料",
        icon: "Document",
      },
      {
        id: 3,
        title: "申请代办",
        description: "代为办理签证申请，提高申请成功率",
        icon: "User",
      },
      {
        id: 4,
        title: "进度跟踪",
        description: "实时跟踪签证申请进度，及时反馈结果",
        icon: "Clock",
      },
    ];

    // 签证申请表单数据
    const visaForm = ref({
      // company: "",
      // contactName: "",
      // position: "",
      // businessScope: "",
      // email: "",

      company: "", // 公司名称
      name: "", // 申请人姓名
      phone: "", // 联系电话
      email: "", //邮箱
      nameEn: "", //申请人英文姓名
      passportNo: "", //护照号
      nationality: "", //国籍
      birthDate: "", //出生日期
      gender: "male", //性别
      position: "", //职务
      entryDate: "", //预计入境日期
      exitDate: "", //预计出镜日期
      purpose: "", //访问目的
    });

    // 获取图标组件
    const getIconComponent = (iconName) => {
      const iconMap = {
        Document: Document,
        Clock: Clock,
        User: User,
        Location: Location,
      };
      return iconMap[iconName] || Document;
    };

    // 提交签证申请
    const submitVisaApplication = async () => {
      try {
        const payload = {
          // company: visaForm.value.company,
          // contactName: visaForm.value.contactName,
          // position: visaForm.value.position,
          // businessScope: visaForm.value.businessScope,
          // email: visaForm.value.email,
          ...visaForm.value,
        };

        console.log("提交签证申请数据:", payload);

        const res = await visaApi.applyVisa(payload);
        console.log("签证申请响应:", res);

        if (res && (res.code === "200" || res.code === 200)) {
          ElMessage.success("签证申请提交成功！我们会尽快与您联系。");
          Object.keys(visaForm.value).forEach((k) => (visaForm.value[k] = ""));
        } else {
          ElMessage.error(res?.msg || "提交失败，请重试");
        }
      } catch (e) {
        console.error("签证申请失败:", e);
        ElMessage.error(e.message || "提交失败，请重试");
      }
    };

    const loadBanner = async () => {
      try {
        const res = await subpageApi.getContentByType("carousel_visitors");
        if (
          res &&
          (res.code === "200" || res.code === 200) &&
          Array.isArray(res.data) &&
          res.data.length > 0
        ) {
          const firstActive =
            res.data.find((item) => item.status === "active") || res.data[0];
          bannerImage.value = firstActive.imageUrl || firstActive.image || "";
        }
      } catch (e) {
        bannerImage.value = "";
      }
    };

    onMounted(() => {
      loadVisaData();
      loadBanner();
    });

    const setActiveTab = (tabKey) => {
      if (!tabKey) return;
      activeTab.value = tabKey;
      if (tabKey !== "visa-system") {
        window.location.href = `/${tabKey}`;
      }
    };

    return {
      activeTab,
      bannerImage,
      pageData,
      visaServices,
      visaForm,
      getIconComponent,
      submitVisaApplication,
      setActiveTab,
    };
  },
};
</script>

<style scoped>
.visa-system {
  min-height: 100vh;
  background: #f8f9fa;
}

.page-header {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  padding: 120px 0 80px 0;
  margin-top: 0;
  text-align: center;
  color: white;
}

.page-header.has-bg {
  height: 400px;
  position: relative;
  background: none;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.page-header.has-bg::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
}

.page-header.has-bg .container {
  position: relative;
  z-index: 1;
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 1rem;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin-bottom: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.visa-section {
  background: white;
  border-radius: 20px;
  padding: 3rem;
  margin: 2rem 0;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 2rem;
  color: #333;
  margin-bottom: 2rem;
  text-align: center;
}

.visa-content {
  max-width: 1000px;
  margin: 0 auto;
}

.visa-intro {
  margin-bottom: 3rem;
}

.visa-intro h3 {
  font-size: 1.8rem;
  color: var(--e-global-color-title);
  margin-bottom: 1rem;
}

.visa-intro p {
  color: #666;
  line-height: 1.8;
  font-size: 1.1rem;
}

.visa-services {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 2rem;
  margin-bottom: 3rem;
}

.service-item {
  display: flex;
  align-items: flex-start;
  padding: 1.5rem;
  background: linear-gradient(135deg, #f8f9ff 0%, #e8f0ff 100%);
  border-radius: 15px;
  border: 1px solid #e0e7ff;
  transition: transform 0.3s ease;
}

.service-item:hover {
  transform: translateY(-3px);
}

.service-icon {
  flex-shrink: 0;
  width: 50px;
  height: 50px;
  background: linear-gradient(
    135deg,
    var(--e-global-color-a3699a4) 0%,
    var(--e-global-color-5c1ca3d) 100%
  );
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 1rem;
}

.service-icon .el-icon {
  font-size: 1.2rem;
  color: white;
}

.service-info h4 {
  font-size: 1.1rem;
  color: var(--e-global-color-title);
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.service-info p {
  color: #666;
  line-height: 1.6;
  font-size: 0.9rem;
  margin: 0;
}

.visa-form {
  background: linear-gradient(135deg, #f8f9ff 0%, #e8f0ff 100%);
  border-radius: 15px;
  padding: 2rem;
  border: 1px solid #e0e7ff;
}

.visa-form h3 {
  font-size: 1.5rem;
  color: var(--e-global-color-title);
  margin-bottom: 2rem;
  text-align: center;
}

.visa-application-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* 移除form-row样式，因为现在都是单列布局 */

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 600;
  color: var(--e-global-color-title);
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 0.8rem;
  border: 1px solid #e0e7ff;
  border-radius: 8px;
  font-size: 1rem;
  transition: border-color 0.3s ease;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: var(--e-global-color-selected);
}

.form-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1rem;
}

.submit-btn {
  background: linear-gradient(
    135deg,
    var(--e-global-color-a3699a4) 0%,
    var(--e-global-color-5c1ca3d) 100%
  );
  color: white;
  border: none;
  padding: 1rem 2rem;
  border-radius: 8px;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 1rem;
}

.submit-btn:hover {
  transform: translateY(-2px);
  background: linear-gradient(
    135deg,
    var(--e-global-color-5c1ca3d) 0%,
    var(--e-global-color-a3699a4) 100%
  );
}

@media (max-width: 768px) {
  .visa-section {
    padding: 2rem;
    margin: 1rem 0;
  }

  .section-title {
    font-size: 1.5rem;
  }

  .visa-services {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }

  /* 移除form-row响应式样式 */

  .page-title {
    font-size: 2rem;
  }

  .page-subtitle {
    font-size: 1rem;
  }
  .form-row {
    grid-template-columns: 1fr;
  }
  .form-group {
    margin-bottom: 0;
  }
}

/* 导航菜单样式 - 参考关于我们页面 */
.nav-menu {
  background: #f8f9fa;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.about-nav {
  display: flex;
  justify-content: center;
  gap: 30px;
  flex-wrap: wrap;
}

.nav-item {
  text-decoration: none;
  color: #333;
  font-size: 1.1rem;
  font-weight: 500;
  padding: 10px 20px;
  border-radius: 8px;
  transition: background-color 0.3s ease, color 0.3s ease;
  border: 1px solid #eee;
  cursor: pointer;
}

.nav-item:hover {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

.nav-item.active {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

@media (max-width: 768px) {
  .about-nav {
    gap: 15px;
  }

  .nav-item {
    font-size: 1rem;
    padding: 8px 16px;
  }
}
</style>
